package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
import com.example.demo.service.FoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * @author Shanks
 * @date 2020-10-22
 */
@Slf4j
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    private final static DateTimeFormatter FORMATTER =  DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Resource
    private BookMapper bookMapper;
    @Resource
    private FoodService foodService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void requiresNewAndAlphaEx() {
        Book book = new Book();
        book.setCode(LocalDateTime.now().format(FORMATTER));
        book.setName("凌凌漆");
        book.setAuthor("星爷");
        book.setPreview("{\"name\":\"007\",\"type\":\"凌凌漆\",\"content\":\"没啥子\"}");
        book.setWordTotal(0L);
        book.setPrintTotal(0L);
        book.setSaleTotal(0L);
        bookMapper.insert(book);

        foodService.saveRequiresNew();

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int i = 1 / 0;
        log.error("A-a方法抛出异常, 预期内部调用B-b方法事务正常");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void requiresNewAndBetaEx() {
        Book book = new Book();
        book.setCode("007");
        book.setName("凌凌漆");
        book.setAuthor("星爷");
        book.setPreview("{\"name\":\"007\",\"type\":\"凌凌漆\",\"content\":\"没啥子\"}");
        book.setWordTotal(0L);
        book.setPrintTotal(0L);
        book.setSaleTotal(0L);
        bookMapper.insert(book);
        log.error("---------- a insert");

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        foodService.saveRequiresNewWithEx();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void nestedAndAlphaEx() {
        Book book = new Book();
        book.setCode("10001");
        book.setName("a异常");
        book.setAuthor("星爷");
        book.setPreview("{\"name\":\"007\",\"type\":\"凌凌漆\",\"content\":\"没啥子\"}");
        book.setWordTotal(0L);
        book.setPrintTotal(0L);
        book.setSaleTotal(0L);
        bookMapper.insert(book);

        foodService.saveNested();

        int i = 1 / 0;
        System.out.println(i);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void nestedAndBetaEx() {
        Book book = new Book();

        foodService.saveNestedWithEx();

        book.setCode("10086");
        book.setName("a事务无异常");
        book.setAuthor("星爷");
        book.setPreview("{\"name\":\"007\",\"type\":\"凌凌漆\",\"content\":\"没啥子\"}");
        book.setWordTotal(0L);
        book.setPrintTotal(0L);
        book.setSaleTotal(0L);
        bookMapper.insert(book);
    }
}